/*--------------------------------------------*
 |  VisualK-OS FBA: Command Constants Header  |
 |  Author: Kevin Slaughter                   |
 | Version: 3.1                               |
 *--------------------------------------------*/

/* Revision History
    v3.1: Optimized/grouped contents.
    v3.0: Initial standardization, after many untracked changes as development progressed.
*/


#ifndef CMDCONSTANTS_H
#define CMDCONSTANTS_H
  /* Includes */
  #include <Arduino.h>
  
  
  /* ##################################
     ##   Serial command constants   ##
     ##  Must mirror python script.  ##
     ################################## */
  // Packet header must precede all commands. Syntax: "!!!^cmd^light^param\n"
  const String ID_STR = "[ID]FBA";    // Flood Bank Array
  const unsigned long COMMS_BAUD_RATE = 115200;
  const static String PACKETHEADER = "!!!";
  
  // Main system-level commands/flags
  const unsigned int CMD_REQ_ID = 255;  // Control center requests ID
  const unsigned int CMD_NONE = 0; const unsigned int CMD_ERROR = 2;
  const unsigned int CMD_OK = 999; const unsigned int CMD_MASS_CFG = 900;
  
  // Binary state lamp control; ON/OFF, no fade in/out.
  const unsigned int CMD_LIGHT_OFF = 11;  // RGB LEDs --- iLight = Lamp index
  const unsigned int CMD_LIGHT_ON = 10;   //              iParam = Fade timer (ms)
  const unsigned int CMD_WF_OFF = 51;     // White Flood LEDs --- iLight = Board #
  const unsigned int CMD_WF_ON = 50;      //                      iParam = Fade timer (ms)
  
  // Set all 4 RGB lamps on board
  // iLight = Target Board   ---   iParam = VKC_? Color
  const unsigned int CMD_ALL_OFF = 21;
  const unsigned int CMD_ALL_ON = 20;
  
  // Enable/disable board-wide color flood
  const unsigned int CMD_FLOOD_ON = 23;
  const unsigned int CMD_FLOOD_OFF = 22;
  
  // Color cfg
  const unsigned int CMD_SET_VKC = 30;    // iParam = VKC_? color index to set RGB lamp to.
  
  // Strobe modes
  // iParam = Strobe program ID
  const unsigned int CMD_STROBE = 18; const unsigned int CMD_STOP_STROBE = 17;
  
  // flashLED()-related flags
  const unsigned int GENERAL_DELAY = 250;
  const unsigned int LED_SOS = 999; const unsigned int LED_OK = 998;
  
  // Mute: Immediate mute of (RGB) iLamp, exempting it from automation routines.
  const unsigned int CMD_MUTE_OFF = 252;
  const unsigned int CMD_MUTE_ON = 253;
  
  // Lamp objects and state consts
  const unsigned int LS_OFF = 0; const unsigned int LS_ON = 1;
  const unsigned int LS_MUTED = 3; const unsigned int LS_STROBE = 14;
  const unsigned int LS_FADE_IN = 20; const unsigned int LS_FADE_OUT = 21;
  
  // Minimums, maximums
  const unsigned int MAX_BRIGHTNESS = 255;   // Highest brightness level (100%) [Max v in HSV = 255]
  const unsigned int MAX_STEP_FADE = 255;    // Highest fade step (100%) ^^^
  
  
  
  /* ###########################
     ### Automation Commands ###
     ########################### */
  // Fade between colors on all boards.
  // iLight = VKC_(From) --- iParam = VKC_(To)
  const unsigned int CMD_COLOR_WALL_OFF = 997;
  const unsigned int CMD_COLOR_WALL_ON = 998;
     
  // Fade in/out
  // iParam = Fade time (ms)
  const unsigned int CMD_FADE_IN = 12;
  const unsigned int CMD_FADE_OUT = 13;
  
  
  
  /* ##############################
     ## Typedef/Obj. definitions ##
     ############################## */
  // VKC_? Color Constants
  const unsigned int VKC_BLUE=1;    const unsigned int VKC_CYAN=2;   const unsigned int VKC_GREEN=3;
  const unsigned int VKC_MAGENTA=4; const unsigned int VKC_ORANGE=5; const unsigned int VKC_PURPLE=6;
  const unsigned int VKC_RED=7;     const unsigned int VKC_WHITE=8;  const unsigned int VKC_YELLOW=9;
  const unsigned int VKC_NONE = 0;  // Function flag for bypassing temp. color assignment
  
  // HSV color struct
  typedef struct {int h; int s; int v;} HsvColor;
  
  // Pseudo lookup table for VKC color associations
  HsvColor VKC2HSV(unsigned int iC) {
      HsvColor hsv;
      switch(iC) {
        case VKC_BLUE:    hsv.h=240; hsv.s=255; hsv.v=255; break;
        case VKC_CYAN:    hsv.h=180; hsv.s=255; hsv.v=255; break;
        case VKC_GREEN:   hsv.h=120; hsv.s=255; hsv.v=255; break;
        case VKC_MAGENTA: hsv.h=300; hsv.s=190; hsv.v=255; break;
        case VKC_ORANGE:  hsv.h=10;  hsv.s=255; hsv.v=255; break;
        case VKC_PURPLE:  hsv.h=300; hsv.s=255; hsv.v=120; break;
        case VKC_RED:     hsv.h=0;   hsv.s=255; hsv.v=255; break;
        case VKC_WHITE:   hsv.h=0;   hsv.s=0;   hsv.v=255; break;
        case VKC_YELLOW:  hsv.h=50;  hsv.s=255; hsv.v=255; break;
      }
      return hsv;
  }
  
  // Lamp object
  typedef struct {
    HsvColor hColor;       // HSV color of RGB lamp
    unsigned int iLamp;    // ShiftPWM index
    unsigned int iState;   // Lamp state
    unsigned int iStep;    // Automation step tracking
    unsigned int iOffset;  // Output correction (Odd-but-necessary wiring)
  } objLamp;
#endif
